package com.fangjinwang.zhushunqing.Widget;

import android.content.Context;
import android.graphics.PointF;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fangjinwang.zhushunqing.R;

import java.util.List;

/**
 *
 */
public class AutoRollViewpager extends ViewPager {
    /**
     * 触摸时按下的点
     **/
    PointF downP = new PointF();
    /**
     * 触摸时当前的点
     **/
    PointF curP = new PointF();
    private boolean isrun = false;
    private int i = 1;
    private Handler handler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(android.os.Message msg) {
            setCurrentItem(getCurrentItem() + msg.what);
        }

        ;
    };
    private Thread thread;
    private ViewPagerItemListener itemListener;

    public AutoRollViewpager(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public AutoRollViewpager(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return true;
    }

    public void setItemListener(ViewPagerItemListener listener) {
        this.itemListener = listener;
    }

//	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//
//		int height = 0;
//		for (int i = 0; i < getChildCount(); i++) {
//			View child = getChildAt(i);
//			child.measure(widthMeasureSpec,
//					MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//			int h = child.getMeasuredHeight();
//			if (h > height)
//				height = h;
//		}
//
//		heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
//				MeasureSpec.EXACTLY);
//
//		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//	}

    public boolean onTouchEvent(MotionEvent arg0) {
        // TODO Auto-generated method stub
        // 每次进行onTouch事件都记录当前的按下的坐标
        curP.x = arg0.getX();
        curP.y = arg0.getY();

        if (arg0.getAction() == MotionEvent.ACTION_DOWN) {
            // 记录按下时候的坐标
            // 切记不可用 downP = curP ，这样在改变curP的时候，downP也会改变
            downP.x = arg0.getX();
            downP.y = arg0.getY();
            // 此句代码是为了通知他的父ViewPager现在进行的是本控件的操作，不要对我的操作进行干扰
            getParent().requestDisallowInterceptTouchEvent(true);
        }

        if (arg0.getAction() == MotionEvent.ACTION_MOVE) {
            float y = arg0.getY();
            float x = arg0.getX();
            if (Math.abs(x - downP.x) > Math.abs(y - downP.y)) {
                // 此句代码是为了通知他的父ViewPager现在进行的是本控件的操作，不要对我的操作进行干扰
                getParent().requestDisallowInterceptTouchEvent(true);

            } else {
                getParent().requestDisallowInterceptTouchEvent(false);


            }
        }
        if (arg0.getAction() == MotionEvent.ACTION_UP) {
            // 在up时判断是否按下和松手的坐标为一个点
            // 如果是一个点，将执行点击事件，
            if (downP.x == curP.x && downP.y == curP.y) {
                if (itemListener != null) {
                    itemListener.OnClick();
                }
                return true;
            }
        }
        return super.onTouchEvent(arg0);
    }

    public void start(final int speed) {
        isrun = true;
        if (thread == null) {
            thread = new Thread() {
                public void run() {
                    while (isrun) {
                        SystemClock.sleep(speed);
                        if (getCurrentItem() == 0
                                || getCurrentItem() == getAdapter().getCount() - 1) {
                            i = -i;
                        }
                        handler.sendEmptyMessage(i);
                    }
                }
            };
            thread.start();
        }
    }

//    private LinearLayout tips;
    private List<ImageView> list;
    public void setIndicator(LinearLayout tips, List<ImageView> list){
//        this.tips=tips;
        this.list=list;
        setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setInditorPosition(position % AutoRollViewpager.this.list.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        setInditorPosition(0);
    }

    public void stop() {
        isrun = false;
        thread = null;
    }

    public void onpause() {
        if (thread != null) {
            try {
                thread.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void onresume() {
        if (thread != null) {
            thread.notifyAll();
        }
    }

    public void setInditorPosition(int selectItems) {
        for (int i = 0; i < list.size(); i++) {
            if (i == selectItems) {
                list.get(i).setImageResource(R.mipmap.huipoint);
            } else {
                list.get(i).setImageResource(R.mipmap.redpoint);
            }
        }
    }



    public interface ViewPagerItemListener {
        public void OnClick();
    }
}
